<?php

/**
 * LEAGUEMan
 * 
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

require_once 'Savant2/Plugin.php';

class Savant2_Plugin_leagueman_show_leaguetable extends Savant2_Plugin{
	
	/**
	 * function plugin
	 * @param object $project LM_Table_Project
	 * @param object $round LM_Table_Round
	 * @param object $tc Tableconfig
	 * @param int $mode 0 = all, 1 = home, 2 = away
	 * @return string 
	 */
	function plugin(LM_Table_Project $project, LM_Table_Round $round, Tableconfig $tc, $mode = 0){
		global $mainframe;
		
		$Itemid = intval(mosGetParam($_REQUEST, "Itemid", 0));
		
		if(!$project instanceof LM_Table_Project) return "LM_Table_Project missing";
		
		if($round instanceof LM_Table_Round){
			$ordering = $round->getOrdering();
		} else {
			$ordering = 0;
		}
		
		$ranking = new LM_Ranking_Football();
		$ranking->setProject($project);
		$ranking->setParam("mode", $mode);
		$ranking->setParam("round", $ordering);
		
		$a = $ranking->getRanking();
		
		$b = array();
        if($tc->getVar("last_ranking") && $ordering > 1){
			$ranking->setParam("round", $ordering - 1);      
            
			$b = $ranking->getRanking();
        }
		
		$colors = $project->getColors();
		
		$min = 1;
		$max = count($a);
		
		$delta = $tc->getVar("delta");
		
		if($delta > 0 && $team = $tc->getVar("fav_team")){
			$i = 1;
			foreach($a AS $key => $val){
				if($key == $team){
					break;
				} else {
					$i++;
				}
			}
			
			$min = $this->getMinimum(count($a), $i, $delta);
			$max = $this->getMaximum(count($a), $i, $delta);
			
		}
	
		ob_start();
?>
			<table width="100%" cellspacing="0">
				<tr>
					<td class="sectiontableheader" align="center"></td>
					<td class="sectiontableheader" align="left"><?php echo _LM_TEAM;?></td>
<?php 
				if($tc->getVar("show_cnt_matches")){ 
?>
					<td class="sectiontableheader" align="center"><?php echo _LM_MATCHES;?></td>
<?php
				}
				if($tc->getVar("show_cnt_v")){ 
?>
					<td class="sectiontableheader" align="center"><?php echo _LM_W;?></td>
<?php
				}
				if($tc->getVar("show_cnt_v1")){ 
?>
					<td class="sectiontableheader" align="center"><?php echo _LM_W1;?></td>
<?php
				}
				if($tc->getVar("show_cnt_v2")){ 
?>
					<td class="sectiontableheader" align="center"><?php echo _LM_W2;?></td>
<?php
				}
				if($tc->getVar("show_cnt_t")){ 
?>
					<td class="sectiontableheader" align="center"><?php echo _LM_D;?></td>
<?php
				}
				if($tc->getVar("show_cnt_d")){ 
?>
					<td class="sectiontableheader" align="center"><?php echo _LM_L;?></td>
<?php
				}
				if($tc->getVar("show_cnt_d1")){ 
?>
					<td class="sectiontableheader" align="center"><?php echo _LM_L1;?></td>
<?php
				}
				if($tc->getVar("show_cnt_d2")){ 
?>
					<td class="sectiontableheader" align="center"><?php echo _LM_L2;?></td>
<?php
				}
				if($tc->getVar("show_results")){ 
?>
					<td class="sectiontableheader" align="center"><?php echo _LM_GOALS;?></td>
<?php
				}
				if($tc->getVar("show_diff")){ 
?>
					<td class="sectiontableheader" align="center"><?php echo _LM_DIFF;?></td>
<?php
				}
				if($tc->getVar("show_points")){
?>
					<td class="sectiontableheader" align="center"><?php echo _LM_POINTS;?></td>
<?php
				}
?>
				</tr>
  
<?php
			$counter = 1;
			$k = 0;
			$j = 0;
			foreach($a as $akey=>$aval){
				
				if($min <= $counter && $max >= $counter){
			
				$z = 0;
				if($tc->getVar("last_ranking") && $ordering > 1){
					$z = 1;
					foreach($b as $bkey=>$bval){
						if($bkey == $akey) break;
						$z++;
					}
				}
				
				if($z > 0){
					$x = $z - $counter;
					
					if($x < 0){
						$img = "arrow_down.gif";
					} else if($x > 0) {
						$img = "arrow_up.gif";
					} else {
						$img = "equal.gif";
					}
					
					$src = $mainframe->getCfg("live_site") . "/components/com_leagueman/images/" . $img;
					$tendence = "&nbsp;<img src=\"$src\" title=\"" . sprintf("(%+d)", $x) . "\" alt=\"\">";
					
				} else {
					$tendence = '';
				}
				
				$class = ($k == 0)? 'sectiontableentry1' : 'sectiontableentry2';
				
				$color = "";
				if(count($colors)){
					if($counter == @$colors[$j]["from"]) $color = @$colors[$j]["color"];
					if($counter > @$colors[$j]["from"] && $counter <= @$colors[$j]["to"]) $color = @$colors[$j]["color"];
					if($counter == @$colors[$j]["to"]) $j++;
				}
				
				$format = "%s";
				if($aval->getTeamId() == $tc->getVar("fav_team")){
					$format = "<b>%s</b>";
				}
				
				if(!function_exists("sefRelToAbs")){
					global $mosConfig_sef;
					require_once $mainframe->getCfg("absolute_path") . "/includes/sef.php";
				}
				$link = sefRelToAbs("index.php?option=com_leagueman&func=teaminfo&tm=" . $aval->getTeammapId() . "&Itemid=" . $Itemid);
    
?>
				<tr class="<?php echo $class;?>">
					<td bgcolor="<?php echo $color;?>"><div align="right"><?php printf($format, $counter); echo ($tc->getVar("last_ranking")) ? $tendence : ''?></div></td>
					<td bgcolor="<?php echo $color;?>">
						<div align="left">
							<a href="<?php echo $link; ?>">
								<?php printf($format, $aval->getTeamname($tc->getVar("shortnames")));?>
							</a>
						</div>
					</td>
<?php
				if($tc->getVar("show_cnt_matches")){ 
?>
					<td bgcolor="<?php echo $color;?>"><div align="center"><?php printf($format, $aval->getMatches());?></div></td>
<?php 
				}
				if($tc->getVar("show_cnt_v")){ 
?>
					<td bgcolor="<?php echo $color;?>"><div align="center"><?php printf($format, $aval->getVictories());?></div></td>
<?php 
				}
				if($tc->getVar("show_cnt_v1")){ 
?>
					<td bgcolor="<?php echo $color;?>"><div align="center"><?php printf($format, 1);?></div></td>
<?php 
				}
				if($tc->getVar("show_cnt_v2")){ 
?>
					<td bgcolor="<?php echo $color;?>"><div align="center"><?php printf($format, 1);?></div></td>
<?php 
				}
				if($tc->getVar("show_cnt_t")){ 
?>
					<td bgcolor="<?php echo $color;?>"><div align="center"><?php printf($format, $aval->getTies());?></div></td>
<?php 
				}
				if($tc->getVar("show_cnt_d")){ 
?>
					<td bgcolor="<?php echo $color;?>"><div align="center"><?php printf($format, $aval->getDefeats());?></div></td>
<?php
				}
				if($tc->getVar("show_cnt_d1")){ 
?>
					<td bgcolor="<?php echo $color;?>"><div align="center"><?php printf($format, 1);?></div></td>
<?php
				}
				if($tc->getVar("show_cnt_d2")){ 
?>
					<td bgcolor="<?php echo $color;?>"><div align="center"><?php printf($format, 1);?></div></td>
<?php
				}
				if($tc->getVar("show_results")){
?>
					<td bgcolor="<?php echo $color;?>"><div align="center"><?php printf($format, sprintf("%s : %s", $aval->getScore1(), $aval->getScore2()));?></div></td>
<?php
				}
				if($tc->getVar("show_diff")){
?>
					<td bgcolor="<?php echo $color;?>"><div align="center"><?php printf($format, sprintf("%+d", $aval->getDiff()));?></div></td>
<?php
				}
				if($tc->getVar("show_points")){
?>
					<td bgcolor="<?php echo $color;?>"><div align="center"><?php printf($format, $aval->getPoints());?></div></td>
<?php
				}
?>
				</tr>
<?php
				}
				
				$k = 1 - $k;
				$counter++;
			}
?>
			</table>

<?php		
			$html = ob_get_contents();
		ob_end_clean();
		
	return $html;
	}
	
	function getMinimum($total, $position, $delta){
	
		if($position <= $delta){
			$x = 1;
		} else if($position >= $total - $delta){
			$x = $total - $delta * 2;
		} else {
			$x = $position - $delta;
		}
	
		return $x;
	}
	
	function getMaximum($total, $position, $delta){
	
		if($position <= $delta){
			$x = $delta * 2 + 1;
		} else if($position >= $total - $delta){
			$x = $total;
		} else {
			$x = $position + $delta;
		}
	
		return $x;
	}
}
?>